#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=110;
int n,m,sum,head[MAXN*MAXN],cnt,tot,ID[MAXN][MAXN],mp[1010];
struct edge
{
	int v,next,val;
}e[MAXN*MAXN*MAXN];
void addedge(int x,int y,int z)
{
	e[cnt]=(edge){y,head[x],z};
	head[x]=cnt++;
	swap(x,y);
	e[cnt]=(edge){y,head[x],0};
	head[x]=cnt++;
	return;
}
struct MF
{
	int idx[MAXN*MAXN],S,T,cur[MAXN*MAXN];
	void bfs()
	{
		memset(idx,0,sizeof(idx));
		queue<int>q;
		q.push(S);
		idx[S]=1;
		while(!q.empty())
		{
			int u=q.front();q.pop();
			for(int i=head[u];~i;i=e[i].next)
			{
				int v=e[i].v;
				if(!e[i].val||idx[v])continue;
				idx[v]=idx[u]+1;
				q.push(v);
			}
		}
		return;
	}
	int dfs(int u,int _min)
	{
		if(u==T)return _min;
		int ret=0,k;
		for(int i=cur[u];~i;i=e[i].next)
		{
			int v=e[i].v;
			cur[u]=i;
			if(!e[i].val||idx[v]!=idx[u]+1)continue;
			k=dfs(v,min(_min-ret,e[i].val));
			e[i].val-=k;
			e[i^1].val+=k;
			ret+=k;
			if(ret==_min)break;
		}
		return ret;
	}
	int dinic()
	{
		int ret=0;
		while(true)
		{
			bfs();
			if(!idx[T])break;
			memcpy(cur,head,sizeof(head));
			ret+=dfs(S,INF);
		}
		return ret;
	}
}T1;
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	n=read();m=read();
	memset(head,-1,sizeof(head));
	int S=0,T=n+n*(n+1)/2+1;
	int tot=n;
	for(int i=1;i<=n;++i)
	{
		int x=read();
		if(!mp[x])mp[x]=++tot,addedge(tot,T,m*x*x);
		addedge(i,mp[x],INF);addedge(i,T,x);
	}
	for(int i=1;i<=n;++i)
		for(int j=i;j<=n;++j)
			ID[i][j]=i==j?i:++tot;
	for(int i=1;i<=n;++i)
		for(int j=i;j<=n;++j)
		{
			int x=read();
			if(i!=j)addedge(ID[i][j],ID[i+1][j],INF),addedge(ID[i][j],ID[i][j-1],INF);
			if(x>0)addedge(S,ID[i][j],x),sum+=x;
			else addedge(ID[i][j],T,-x);
		}
	T1.S=S;T1.T=T;
	printf("%d\n",sum-T1.dinic());
	return 0;
}
